/* NOTE: this do file calculates predictive efficiency for conditional logit models */


capture program drop calcROC_clogit
program define calcROC_clogit, eclass

	capture drop p1
	capture predict p1 if e(sample)
	order p1, after(pm_party_probability)
	
	capture drop p2
	capture egen double p2 = max(p1) if e(sample), by(cabinet_id)
	order p2, after(p1)
	
	capture drop p3
	capture gen p3=0 if e(sample)
	replace p3=1 if p2==p1 
	order p3, after(p2)
	
	
	/* calculate predictive efficiency */
	
	
	capture drop truep 
	capture gen truep=p3*prime_minister if e(sample)
	
	capture drop truen
	capture gen truen=(1-p3)*(1-prime_minister) if e(sample)
	
	capture drop falsep
	capture gen falsep=p3*(1-prime_minister) if e(sample)
	
	capture drop falsen
	capture gen falsen=(1-p3)*prime_minister if e(sample)
	
	quietly summarize truep 
	local sumtruep = r(sum)
	
	quietly summarize truen
	local sumtruen = r(sum)
	
	quietly summarize falsep
    local sumfalsep = r(sum)
	
	quietly summarize falsen
	local sumfalsen = r(sum)

	local tpr=`sumtruep'/(`sumtruep'+`sumfalsen')
	local fpr=`sumfalsep'/(`sumtruen'+`sumfalsep')
    local accuracy=(`sumtruep'+`sumtruen')/(`sumtruep'+`sumtruen'+`sumfalsep'+`sumfalsen')

	unique cabinet_id if e(sample)
	
	display "truep: `sumtruep', truen: `sumtruen', falsep: `sumfalsep', falsen: `sumfalsen'" 
	display "true positive rate: `tpr', false positive rate: `fpr'"
	display "accuracy: `accuracy'"
	
	ereturn scalar NFO = `sumtruep'+`sumfalsen'
	ereturn scalar TP = `sumtruep' 
	ereturn scalar TPR = `tpr'
	
end
